import Vue from "vue";
import Vuex from "vuex";
import settings from "@/settings"
import utils from "@/utils/index"
import login from '@/api/login/index'
import router from "@/router"
import { setToken } from "@/utils/auth"

Vue.use(Vuex);
export default new Vuex.Store({
  state: {
    collapse: true,
    title: settings.title,
    menuData: [
      {
        name: "首页",
        path: "/home",
        icon: "el-icon-house",
        children: []
      }, {
        name: "用户管理",
        path: "/user",
        icon: "el-icon-s-custom",
        children: []
      }, {
        name: "角色管理",
        path: "/role",
        icon: "el-icon-user",
        children: []
      }, {
        name: "菜单管理",
        path: "/menu",
        icon: "el-icon-s-order",
        children: []
      }, {
        name: "组织结构",
        path: "/organChart",
        icon: "el-icon-data-analysis",
        children: []
      }, {
        name: "图表管理",
        path: "/echarts",
        icon: "el-icon-s-marketing",
        children: []
      }, {
        name: "系统管理",
        path: "/system",
        icon: "el-icon-s-order",
        children: [{
          path: "/log",
          name: "日志管理",
          icon: "",
          children: []
        }]
      }, {
        path: "/map",
        name: "地图分类",
        icon: "el-icon-s-promotion",
        children: [{
          path: "/cesium",
          name: "铯地图",
          icon: "",
          children: []
        }, {
          path: "/tencent",
          name: "腾讯地图",
          icon: "",
          children: []
        }, {
          path: "/openlayer",
          name: "openlayer",
          icon: "",
          children: []
        }, {
          path: "/starMap",
          name: "地图闪点",
          icon: "",
          children: []
        }]
      }, {
        name: "布局汇总",
        path: "/layout",
        icon: "el-icon-s-operation",
        children: [{
          path: "/flex",
          name: "flex布局",
          icon: "",
          children: []
        }, {
          path: "/cup",
          name: "圣杯布局",
          icon: "",
          children: []
        }]
      }, {
        name: "测试案例",
        path: "/summarize",
        icon: "el-icon-c-scale-to-original",
        children: [{
          path: "/css",
          name: "特效案例",
          icon: "",
          children: []
        }, {
          path: "/sidebar",
          name: "隐藏侧边栏",
          icon: "",
          children: []
        }, {
          path: "/tableCompare",
          name: "表格对比",
          icon: "",
          children: []
        }, {
          path: "/components",
          name: "组件汇总",
          icon: "",
          children: []
        }, {
          path: "/test",
          name: "测试案例",
          icon: "",
          children: []
        }]
      }
    ],
    name: '',
    token: '',
    tabViewList: [{
      name: 'home',
      path: "/home",
      meta: {
        fixed: true,
        title: "首页"
      }
    }],
    cachedViews: []
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    ADD_TAB: (state, view) => {
      if (state.tabViewList.some(v => v.path === view.path)) return false
      state.tabViewList.push(
        Object.assign({}, view)
      )
    },
    CLOSE_TAB: (state, tab) => {
      utils.removeByValue(state.tabViewList, "path", tab.path)
      if (router.currentRoute.path == tab.path) {
        const len = state.tabViewList.length
        if (len > 0) {
          router.push({ path: state.tabViewList[len - 1].path })
        } else {
          router.push({ path: '/home' })
        }
      }
    },
    ADD_CACHED_VIEW: (state, view) => {
      if (state.cachedViews.includes(view.name)) {
        return
      }
      state.cachedViews.push(view.name)
    },
    DEL_CACHED_VIEW: (state, view) => {
      const index = state.cachedViews.indexOf(view.name)
      index > -1 && state.cachedViews.splice(index, 1)
    },
    COLLAPSE: (state) => {
      state.collapse = !state.collapse
    }
  },
  actions: {
    login ({ commit }, userInfo) {
      const { username, password } = userInfo
      return new Promise((resolve, reject) => {
        login.login({ username: username.trim(), password: password }).then(response => {
          if (response.status === 1) {
            const { data } = response
            if (data.verifySuccess) {
              commit('SET_TOKEN', data.userInfo.token)
              setToken(data.userInfo.token)
            }
            resolve(data.verifySuccess)
          }
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
});